<!doctype html>
<meta charset="utf-8">
<title>Content Index: Behaviour of the add() function</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../serviceworker/resources/test-helpers.js"></script>
<script src="resources.js"></script>
<script>
'use strict';

contentIndexTest(async (t, index) => {
  await expectTypeErrorWithMessage(
      index.add(createDescription({id: ''})),
      `Failed to execute 'add' on 'ContentIndex': ID cannot be empty`);
  await expectTypeErrorWithMessage(
      index.add(createDescription({title: ''})),
      `Failed to execute 'add' on 'ContentIndex': Title cannot be empty`);
  await expectTypeErrorWithMessage(
      index.add(createDescription({description:''})),
      `Failed to execute 'add' on 'ContentIndex': Description cannot be empty`);
  await expectTypeErrorWithMessage(
      index.add(createDescription({category: 'fake-category'})));

  await expectTypeErrorWithMessage(
      index.add(createDescription({iconUrl: 'file://some-local-file.png'})),
      `Failed to execute 'add' on 'ContentIndex': Invalid icon URL protocol`);
  await expectTypeErrorWithMessage(
      index.add(createDescription({includeIcons: false})),
      'icons must be provided');
  await expectTypeErrorWithMessage(
      index.add(createDescription({iconUrl: '/non-existent-icon.png'})),
      'Icon could not be loaded');
  await expectTypeErrorWithMessage(
      index.add(createDescription({iconUrl: '/resources/dummy.txt'})),
      'Icon could not be loaded');

  await expectTypeErrorWithMessage(
      index.add(createDescription({url: 'https://other-domain.com/'})),
      `Failed to execute 'add' on 'ContentIndex': Service Worker cannot request provided launch URL`);
  await expectTypeErrorWithMessage(
      index.add(createDescription({url: '/different-scope'})),
      `Failed to execute 'add' on 'ContentIndex': Launch URL must belong to the Service Worker's scope`);

  await index.add(createDescription({}));

}, 'index.add parameters are validated.');

contentIndexTest(async (t, index) => {
  // Register an entry.
  await index.add(createDescription({id: 'my-id'}));

  // Simulate a user deleting it.
  testRunner.simulateWebContentIndexDelete('my-id');

  const swMessage = await waitForMessageFromServiceWorker();
  assert_equals(swMessage, 'my-id');

  // Make sure it is actually deleted.
  const descriptions = await index.getAll();
  assert_equals(descriptions.length, 0);

}, 'Fire contentdelete event on user intiated content deletion.');

contentIndexTest(async (t, index) => {
  // Register an entry.
  await index.add(createDescription({id: 'register-again'}));

  // Simulate a user deleting it.
  testRunner.simulateWebContentIndexDelete('register-again');

  const swMessage = await waitForMessageFromServiceWorker();
  assert_equals(swMessage, 'Failed to add description due to I/O error.');

  // Make sure it is actually deleted and no new content was added.
  const descriptions = await index.getAll();
  assert_equals(descriptions.length, 0);

}, 'Content cannot be registered while contentdelete event is firing.');

</script>
